package genetic.q4.functions;

import genetic.Function;
import genetic.TreeNode;
import genetic.q4.TicTacToeBoard;

public class IfXClearPutElse extends TicTacToeFunction{

	int x;

	/**
	 * constructor for prototype only (NOT TO BE USED)
	 */
	public IfXClearPutElse() {
		super(null);
	}

	public IfXClearPutElse(int x, TreeNode doElse){
		super(new TreeNode[]{doElse});
		this.x = x;
	}

	@Override
	public Function createInstance(TreeNode[] kids) {
		return new IfXClearPutElse(x, kids[0]);
	}

	@Override
	public int getNumKids() {
		return 1;
	}

	@Override
	public TreeNode createRandomInstance(TreeNode[] kids) {
		int rand = (int)(Math.random()*9);
		return new IfXClearPutElse(rand, kids[0]);
	}

	@Override
	public void play(TicTacToeBoard board, int side) {
		if (!board.put(side, x)){
			if (kids[0].isFunction()){
				((TicTacToeFunction)kids[0]).play(board, side);
			} else {
				((TicTacToeTerminal)kids[0]).play(board, side);
			}
		}
	}

	public int getIndex() {
		return x;
	}

	@Override
	public String getName() {
		return "if"+x+"cpe";
	}

}
